package Database;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Vector;

public class SanPham 
{
    private String masp;
    private String tensp;
    private double gia;
    private int soluong;
    private java.util.Date ngaynhap;
    private String mota;
    private String madm;

    /**
     * @return the masp
     */
    public String getMasp() {
        return masp;        
    }

    /**
     * @param masp the masp to set
     */
    public void setMasp(String masp) {
        this.masp = masp;
    }

    /**
     * @return the tensp
     */
    public String getTensp() {
        return tensp;
    }

    /**
     * @param tensp the tensp to set
     */
    public void setTensp(String tensp) {
        this.tensp = tensp;
    }

    /**
     * @return the gia
     */
    public double getGia() {
        return gia;
    }

    /**
     * @param gia the gia to set
     */
    public void setGia(double gia) {
        this.gia = gia;
    }

    /**
     * @return the soluong
     */
    public int getSoluong() {
        return soluong;
    }

    /**
     * @param soluong the soluong to set
     */
    public void setSoluong(int soluong) {
        this.soluong = soluong;
    }

    /**
     * @return the ngaynhap
     */
    public java.util.Date getNgaynhap() {
        return ngaynhap;
    }

    /**
     * @param ngaynhap the ngaynhap to set
     */
    public void setNgaynhap(java.util.Date ngaynhap) {
        this.ngaynhap = ngaynhap;
    }

    /**
     * @return the mota
     */
    public String getMota() {
        return mota;
    }

    /**
     * @param mota the mota to set
     */
    public void setMota(String mota) {
        this.mota = mota;
    }

    /**
     * @return the madm
     */
    public String getMadm() {
        return madm;
    }

    /**
     * @param madm the madm to set
     */
    public void setMadm(String madm) {
        this.madm = madm;
    }
    
    public static Vector<Vector> DanhSachSP(String madm)
    {
        Vector<Vector> dssp = new Vector<Vector>();        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from sanpham where madm = '"+madm+"'";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
                Vector<Object> sp = new Vector<Object>();
                sp.add(rs.getString("masp"));
                sp.add(rs.getString("tensp"));
                sp.add(rs.getDouble("gia"));
                sp.add(rs.getInt("soluong"));
                sp.add(rs.getDate("ngaynhap"));
                sp.add(rs.getString("mota"));
                dssp.add(sp);
            }
        }
        catch(Exception ex)
        {
            dssp = null;
        }
        
        return dssp;
    }
    
    
    public boolean ThemSP()
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "insert into sanpham values(?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, this.masp);
            ps.setString(2, this.tensp);
            ps.setDouble(3, this.gia);
            ps.setInt(4, this.soluong);
            java.sql.Date dns = new Date(this.ngaynhap.getTime());
            ps.setDate(5, dns);
            ps.setString(6, this.mota);
            ps.setString(7, this.madm);
            kq = ps.executeUpdate() > 0;
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            kq = false;
        }
        
        return kq;
    }
    
}
